<template>
  <div class="order-list">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <div class="top">
        <div>
          <el-form-item label="订单号">
            <el-input v-model="formInline.order" placeholder="订单号"></el-input>
          </el-form-item>
          <el-form-item label="收货人">
            <el-input v-model="formInline.user" placeholder="收货人"></el-input>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="formInline.cellPhone" placeholder="手机号"></el-input>
          </el-form-item>
          <el-form-item label="订单状态">
            <el-select v-model="formInline.region" placeholder="订单状态">
              <el-option label="未受理" value="未受理"></el-option>
              <el-option label="已受理" value="已受理"></el-option>
              <el-option label="配送中" value="配送中"></el-option>
              <el-option label="已完成" value="已完成"></el-option>
            </el-select>
          </el-form-item>
        </div>

        <el-form-item label="选择时间">
          <el-date-picker
            v-model="formInline.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
          <el-button @click="inquire" class="cx" type="primary">查询</el-button>
          <el-button @click="reset" type="success">重置</el-button>
        </el-form-item>
      </div>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="订单号"> </el-table-column>
        <el-table-column prop="deliveryTime" label="下单时间"> </el-table-column>
        <el-table-column prop="phone" label="手机号"> </el-table-column>
        <el-table-column prop="consignee" label="收货人"> </el-table-column>
        <el-table-column prop="deliverAddress" label="配送地址"> </el-table-column>
        <el-table-column prop="orderTime" label="送达时间"> </el-table-column>
        <el-table-column prop="remarks" label="用户备注"> </el-table-column>
        <el-table-column prop="orderAmount" label="订单金额"> </el-table-column>
        <el-table-column prop="orderState" label="订单状态"> </el-table-column>

        <!-- 操作 -->
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button @click="handleEdit(scope.row)" type="text" size="small">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[1, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 模态框 -->
    <el-dialog title="收货地址" :visible.sync="dialogTableVisible">
      <el-table :data="gridData">
        <el-table-column property="orderNo" label="订单号" ></el-table-column>
        <el-table-column property="deliveryTime" label="下单时间" ></el-table-column>
        <el-table-column property="phone" label="手机号"></el-table-column>
        <el-table-column property="consignee" label="收货人"></el-table-column>
        <el-table-column property="deliverAddress" label="配送地址"></el-table-column>
        <el-table-column property="orderTime" label="送达时间"></el-table-column>
        <el-table-column property="remarks" label="用户备注"></el-table-column>
        <el-table-column property="orderAmount" label="订单金额"></el-table-column>
        <el-table-column property="orderState" label="订单状态"></el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getorderList } from "@/api/order";
import { normalizedate } from "@/utils/tools";
import local from '@/utils/local'
export default {
  data() {
    return {
       gridData: [],
      dialogTableVisible: false,
      formInline: {
        order: "",
        user: "",
        cellPhone: "",
        region: "",
        date: [],
      },
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
    };
  },
  methods: {
    inquire() {
      this.currentPage = 1;
      this.getData();
    },
    getData() {
      getorderList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        orderNo: this.formInline.order,
        consignee: this.formInline.user,
        phone: this.formInline.cellPhone,
        orderState: this.formInline.region,
        date: JSON.stringify(this.formInline.date),
      })
        .then((res) => {
          let { total, data } = res.data;

          data.forEach((item) => {
            item.orderTime = normalizedate(item.orderTime);
            item.deliveryTime = normalizedate(item.deliveryTime);
          });

          this.tableData = data;
          this.total = total;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    reset() {
      this.formInline = {
        order: "",
        user: "",
        cellPhone: "",
        region: "",
        date: [],
      };
    },
    handleClick(row) {
      this.dialogTableVisible=true
      console.log(row);
      this.gridData.push(row)
    },
    // 每页条数变化
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    // 编辑
    handleEdit(row) {
      console.log(row);
      local.set('orderInfo', row)
      this.$router.push("/order/order-edit"); // 跳转到订单编辑页面
    },
    // 页码变化
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.order-list {
  height: 100%;
  background-color: #fff;
}
/deep/.cx {
  margin-left: 10px;
}
.top {
  padding-left: 10px;
  padding-top: 10px;
}
.el-pagination {
  margin-top: 20px;
}
</style>
